package com.culture.bootdemo.common.filter;


import com.google.common.collect.Sets;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Set;

/**
 * @author ：wen
 * @date ：Created in 2019/8/7 14:28
 * @description：:
 */
@Slf4j
@Component
@WebFilter(filterName = "apiFilter",
        urlPatterns = {"/*"})
public class ApiFilter implements Filter {


    private final static Set<String> IGNORE_URI = Sets.newConcurrentHashSet();

    static {
        IGNORE_URI.add("/test/stopWatch");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String path = request.getRequestURI().substring(request.getContextPath().length()).replaceAll("[/]+$", "");
        //此处的path已经去掉了 contextPath
        if (!IGNORE_URI.contains(path)) {
            filterChain.doFilter(request, response);
        }
    }

}
